package com.archermind.boc.xml.parser.impl;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.archermind.boc.Logger;
import com.archermind.boc.xml.ApplicationContextFactory;
import com.archermind.boc.xml.parser.Processor;

public class XMLParser {
	private final static Logger logger = Logger.getLogger(XMLParser.class.getName());
	private ClassPathXmlApplicationContext factory = ApplicationContextFactory.getContext();

	public void parse(String sessionID, String xml) {
		logger.info("[request in]sessionID:" + sessionID + ",xml=" + xml);
		Processor processor = null;
		if (xml.startsWith("<auth")) {
			processor = (Processor) factory.getBean("auth");
		} else if (xml.startsWith("<iq")) {
			processor = (Processor) factory.getBean("iq");
		} else if (xml.startsWith("<message")) {
			processor = (Processor) factory.getBean("message");
		} else if (xml.startsWith("<presence")) {
			processor = (Processor) factory.getBean("presence");
		} else {
			logger.info("Error when parse xml,not support protocol!");
		}
		if (processor != null) {
			processor.process(sessionID, xml);
		}
	}
}
